package sort.select;

import java.util.Arrays;

public class SelectSort {

	public int pos;

	public int total;

	public boolean isOK;

	public int[] selectSort1(int[] array) {
		int temp;
		total = 0;
		//外层循环，意味着排序次数
		for (int i = 0; i < array.length - 1; i++) {
			//假设array[i]元素最小，记录下标
			pos = i;
			//内层循环，意味着比较次数
			for (int j = i+1; j < array.length; j++) {
				//下标设置为最小
				if (array[j] < array[pos]) {
					pos = j;
				}
			}
			total++;
			//交换元素，将最小值移动到最前
			temp = array[i];
			array[i] = array[pos];
			array[pos] = temp;
		}
		return array;
	}

	public int[] selectSort2(int[] array) {
		int temp;
		total = 0;
		//外层循环，意味着排序次数
		for (int i = 0; i < array.length - 1; i++) {
			pos = i;
			isOK = true;
			//内层循环，意味着比较次数
			for (int j = i+1; j < array.length; j++) {
				//下标设置为最小
				if (array[j] < array[pos]) {
					pos = j;
					isOK = false;
				}
			}
			total++;
			if (!isOK) {
				temp = array[i];
				array[i] = array[pos];
				array[pos] = temp;
			}
		}
		return array;
	}

	public static void main(String[] args) {
		SelectSort test = new SelectSort();
		int array[];
		int array1[] = {1,5,2,8,9,3};
		int array2[] = {1,5,2,8,9,3};
		System.out.println("原数组：" + Arrays.toString(array1));
		System.out.println("原数组：" + Arrays.toString(array2));

		array = test.selectSort1(array1);
		System.out.println("SelectSort1：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);

		array = test.selectSort2(array2);
		System.out.println("SelectSort2：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);
	}

}
